<?php
namespace Home\Controller;
/**
 * 购物车类
 * User: linkwhy
 * Date: 2015/10/23
 * Time: 10:50
 */
class CartController extends HomeController{

    public $wechat_id = '';
    public $goods_id = '';
    public $cart = '';

    public function _initialize(){

        $this->wechat_id = $_SESSION['wechat_id'];
        $this->goods_id = I('get.goods_id');
        $this->cart = M('cart');

    }

    public function index(){
        $list = $this->cart->field('caitou_cart.id,goods_id,num,name,price,picture,class_id,shop_price,color,size,zhekou,foot_width,foot_long,foot_thick')
            ->join('__GOODS__ ON __CART__.goods_id = __GOODS__.id')
            ->where(array('is_create' => 0, 'client_id' => $this->wechat_id))
            ->select();

        $cartAllPrice = 0;  //购物车总价
        $saveMoney = 0;     //节约好多钱
        //foreach($list as $key => &$val){
            $cartAllPrice += floatval($list[0]['price']) * intval($list[0]['num']);
            $saveMoney += ( floatval($list[0]['price']) * intval($list[0]['num']) ) * ( intval($list[0]['zhekou'])/100*0.4 );
        //}
        $data = array('cartAllPrice' => $cartAllPrice, 'saveMoney' => $saveMoney, 'name' => '购物车', 'class_id' => $_SESSION['class_id']);
        $this->assign('data', $data);
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * 加入购物车
     */
    public function addCart(){
        $color = I('get.color_value');
        $size = I('get.size', 0);
        $cart_id = $this->cart->where(array('client_id' => $this->wechat_id, 'goods_id' => $this->goods_id, 'is_create' => 0, 'color' => $color, 'size' => $size))->getField('id');
        $foot_width = I('get.foot_width', '');
        $foot_long = I('get.foot_long', '');
        $foot_thick = I('get.foot_thick', '');
        if( $this->saveUserSize($foot_width, $foot_long, $foot_thick) ){
            if($cart_id > 0){
//                //购物车有此信息数量+1
                $num = I('get.num',1);
                $this->cart->where(array('id' => intval($cart_id)))->setInc('num', $num);
                $this->ajaxReturn(array('status' => true, 'info' => '加入成功！', 'is_add' => 'yes'));
            }else{
                //购物车没有此信息
                $get_data['client_id'] = $this->wechat_id;
                $get_data['goods_id'] = $this->goods_id;
                $get_data['num'] = I('get.num');

                $this->cart->create($get_data);
                if($this->cart->add()){
                    $this->ajaxReturn(array('status' => true, 'info' => '加入成功！', 'is_add' => 'yes'));
                }else{
                    $this->ajaxReturn(array('status' => false));
                }
            }
        }

    }

    /**
     * 修改购物车数量
     */
    public function update_num(){
        $id = I('get.id');
        if($this->cart->where(array('id' => $id))->setField('num', I('get.num'))){
            $this->ajaxReturn(array('status' => true));
        }else{
            $this->ajaxReturn(array('status' => false));
        }
    }

    /**
     * 删除购物车商品
     */
    public function del_goods(){
//        $type = I('get.type');      //删除类型


        if($this->cart->where(array('id' => I('post.id')))->delete()){
            $this->ajaxReturn(array('status' => true));
        }
//        switch($type){
//            case 1:
//                //单个删除
//                if($this->cart->where(array('id' => I('get.id')))->delete()){
//                    $this->ajaxReturn(array('status' => true));
//                }
//                break;
//            case 2:
//                //清空购物车
//                if($this->cart->where(array('client_id' => $this->wechat_id, 'is_create' => 0))->delete()){
//                    $this->ajaxReturn(array('status' => true));
//                }
//                break;
//        }
    }

    /**
     * 保存用户尺寸
     */
    public function saveUserSize($foot_width, $foot_long, $foot_thick){
        $client = M('clients');
        $data = $client->field('foot_width, foot_long, foot_thick')->where(array('uid' => $_SESSION['userid']))->find();
        if( $data['foot_width'] == $foot_width && $data['foot_long'] == $foot_long && $data['foot_thick'] == $foot_thick ){
            return true;
        }else{
            $getData = array(
                'foot_width' => $foot_width,
                'foot_long' => $foot_long,
                'foot_thick' => $foot_thick
            );
            if( $client->where(array('uid' => $_SESSION['userid']))->save($getData) ){
                return true;
            }else{
                return false;
            }
        }
    }

}